package hydrap2p.download;

import hydrap2p.ServiceLocation;
import hydrap2p.library.Range;

import java.util.Arrays;
import java.util.List;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

/**
 * The Class ProtocolAdapter.
 */
@SuppressWarnings("serial")
public class ProtocolAdapter extends Protocol implements DownloadProtocol.Iface {
	private boolean active;
	
	/**
	 * Instantiates a new protocol adapter.
	 * 
	 * @param location the location
	 * @param type the type
	 * @throws TTransportException the t transport exception
	 */
	public ProtocolAdapter(ServiceLocation location, String type) throws TTransportException {
		this.location = location;
		this.type = type;
		this.active = true;
	}
	
	/* (non-Javadoc)
	 * @see hydrap2p.download.DownloadProtocol.Iface#cancelDownload(long, hydrap2p.download.Source)
	 */
	@Override
	public void cancelDownload(long uid, Source source) throws TException {
		TTransport transport = new TSocket(location.getAddress(), location.getPort());
		DownloadProtocol.Client client = new DownloadProtocol.Client(new TBinaryProtocol(transport));
		transport.open();
		
		client.cancelDownload(uid, source);
		
		transport.close();
	}

	/* (non-Javadoc)
	 * @see hydrap2p.download.DownloadProtocol.Iface#download(long, hydrap2p.download.Source, hydrap2p.library.Range)
	 */
	@Override
	public void download(long uid, Source source, Range range) throws TException {
		TTransport transport = new TSocket(location.getAddress(), location.getPort());
		DownloadProtocol.Client client = new DownloadProtocol.Client(new TBinaryProtocol(transport));
		transport.open();
		try {
			client.download(uid, source, range);
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		transport.close();
		
		return;
	}

	/* (non-Javadoc)
	 * @see hydrap2p.Service.Iface#ping()
	 */
	@Override
	public void ping() throws TException {
		TTransport transport = new TSocket(location.getAddress(), location.getPort());
		DownloadProtocol.Client client = new DownloadProtocol.Client(new TBinaryProtocol(transport));
		transport.open();
		
		client.ping();
		
		transport.close();
	}

	/* (non-Javadoc)
	 * @see hydrap2p.Service.Iface#supportedInterfaces()
	 */
	@Override
	public List<String> supportedInterfaces() throws TException {
		return Arrays.asList("DownloadProtocol");
	}
	
	/* (non-Javadoc)
	 * @see hydrap2p.download.Protocol#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Protocol) {
			Protocol adapter = (Protocol)obj;
			
			return this.location.equals(adapter.location);
		}
		
		return false;
	}
	
	/* (non-Javadoc)
	 * @see hydrap2p.download.Protocol#hashCode()
	 */
	@Override
	public int hashCode() {
		return location.hashCode();
	}

	/**
	 * Sets the active.
	 * 
	 * @param active the active to set
	 */
	public void setActive(boolean active) {
		this.active = active;
	}

	/**
	 * Checks if is active.
	 * 
	 * @return the active
	 */
	public boolean isActive() {
		return active;
	}
}
